package com.xiaoyun.service.impl;

import com.xiaoyun.mapper.CategoryMapperCustom;
import com.xiaoyun.pojo.Category;
import com.xiaoyun.repository.CategoryRepository;
import com.xiaoyun.service.CategoryService;
import com.xiaoyun.vo.CategoryVO;
import com.xiaoyun.vo.NewItemsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {


    private final CategoryRepository categoryRepository;

    private final CategoryMapperCustom categoryMapperCustom;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    @Cacheable(value = "categoryService", key = "'_queryAllRootLevelCat'")
    public List<Category> queryAllRootLevelCat() {
        List<Category> result = categoryRepository.findByType(1);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    @Cacheable(value = "categoryService",key = "#rootCatId +'_getSubCatList'")
    public List<CategoryVO> getSubCatList(Integer rootCatId) {
        return categoryMapperCustom.getSubCatList(rootCatId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    @Cacheable(value = "categoryService",key = "#rootCatId+'_getSixNewItemLazy'")
    public List<NewItemsVO> getSixNewItemLazy(Integer rootCatId) {
        Map<String, Object> map = new HashMap<>();
        map.put("rootCatId", rootCatId);
        return categoryMapperCustom.getSixNewItemsLazy(map);
    }
}
